package com.config;

import com.framework.MessageConverter.UserMessageConverter;
import com.messageHandler.PersonMessageHandler;
import com.service.ISayHello;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.apache.activemq.spring.ActiveMQConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.jms.listener.adapter.MessageListenerAdapter;
import org.springframework.jms.remoting.JmsInvokerProxyFactoryBean;
import org.springframework.jms.support.converter.MessageConverter;

import java.util.ArrayList;

@Configuration
@ComponentScan(basePackages = {"com.messageHandler"})
public class JMSService {
    @Autowired
    private Environment environment;

    //=========================消息=======================
    @Bean
    public ActiveMQConnectionFactory activeMQConnectionFactory() {
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
        activeMQConnectionFactory.setBrokerURL( environment.getProperty( "activeMQ.brokerUrl" ) );
        activeMQConnectionFactory.setTrustAllPackages( true ); //activeMQ针对对象的传输有白名单限制（即使你的类实现了序列化借口也会报错），需要将你类所在的包加入白名单。在接受的时候将activeMQConnection.setTrustAllPackages(true);设置为true，（默认的是fales）。
        return activeMQConnectionFactory;
    }

    //=================activeMQQueue====================
    @Bean
    public ActiveMQQueue queue() {
        ActiveMQQueue queue = new ActiveMQQueue( environment.getProperty( "activeMQ.message.queue.name" ) );
        return queue;
    }

    //=================activeMQTopic====================
    @Bean
    public ActiveMQTopic topic() {
        ActiveMQTopic topic = new ActiveMQTopic( environment.getProperty( "activeMQ.message.topic.name" ) );
        return topic;
    }

    //=====================message converter===========
    @Bean
    public MessageConverter mappingJacksonMessageConverter() {
        return new UserMessageConverter();
    }


    @Bean
    public JmsTemplate jmsTemplate(ActiveMQConnectionFactory activeMQConnectionFactory, MessageConverter messageConverter) {
        JmsTemplate jmsTemplate = new JmsTemplate( activeMQConnectionFactory );
        jmsTemplate.setMessageConverter( messageConverter );
        return jmsTemplate;
    }


    @Bean
    public MessageListenerAdapter messageListenerAdapter(PersonMessageHandler personMessageHandler) {
        MessageListenerAdapter messageListenerAdapter = new MessageListenerAdapter( personMessageHandler );
        messageListenerAdapter.setDefaultListenerMethod( "handleMessage" );
        return messageListenerAdapter;
    }

    @Bean
    public DefaultMessageListenerContainer messageListenerContainer(ActiveMQConnectionFactory activeMQConnectionFactory, MessageConverter messageConverter, MessageListenerAdapter messageListenerAdapter) {
        DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
        messageListenerContainer.setConnectionFactory( activeMQConnectionFactory );
        messageListenerContainer.setMessageConverter( messageConverter );
        messageListenerContainer.setDestinationName( environment.getProperty( "activeMQ.message.queue.name" ) );
        messageListenerContainer.setMessageListener( messageListenerAdapter );
        return messageListenerContainer;
    }

    //===============================JmsInvokerProxy =============
    @Bean
    public JmsInvokerProxyFactoryBean sayHelloService(org.apache.activemq.ActiveMQConnectionFactory activeMQConnectionFactory, MessageConverter messageConverter){
        JmsInvokerProxyFactoryBean jmsInvokerProxyFactoryBean = new JmsInvokerProxyFactoryBean();
        jmsInvokerProxyFactoryBean.setServiceInterface( ISayHello.class );
        jmsInvokerProxyFactoryBean.setConnectionFactory(activeMQConnectionFactory  );
        jmsInvokerProxyFactoryBean.setMessageConverter( messageConverter );
        jmsInvokerProxyFactoryBean.setQueueName(  environment.getProperty( "activeMQ.service.queue.name" ));
        return jmsInvokerProxyFactoryBean;
    }
}
